package com.lan.controller.spike;

import com.lan.utils.cache.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <p>
 *  秒杀
 * </p>
 *
 * @author shadow
 * @since 2022-09-23
 */
@Controller
@RequestMapping("/spike")
public class SpikeController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SpikeController.class);
    private static final String TICKET = "ticket";

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 活动开始时间
     */
    public LocalDateTime getStartTime() {
        return null;
    }

    /**
     * 创建订单
     */

    public String createOrder() {
        return null;
    }

    /**
     * 下单
     */
    public void spike() {

        int ticket = Integer.parseInt(Objects.requireNonNull(stringRedisTemplate.opsForValue().get(TICKET)));
        if (ticket > 0) {
            LOGGER.info("抢到了 : {}, 剩余 : {}", ticket, --ticket);
            stringRedisTemplate.opsForValue().set(TICKET, ticket + "");
        } else {
            LOGGER.info("没抢到 -   库存不足");
        }
    }
}
